package com.briup.cms.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.briup.cms.bean.User;
import com.briup.cms.bean.extend.UserExtend;
import com.briup.cms.service.RoleService;
import com.briup.cms.service.UserService;
import com.briup.cms.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author briup
 * @since 2023-08-31
 */
@Api(tags = "用户模块")
@RestController
@Slf4j
@RequestMapping("/auth/user")
public class UserController {
    @Autowired
    private UserService userService;


    //admin使用：用户登录后首页展示个人信息需要使用
    @ApiOperation("获取用户个人信息")
    @GetMapping("/info")  //    @RequestAttribute 请参考JwtInterceptor中 38行代码
    public Result getInfo(@RequestAttribute("userId") Long id) {
        log.info("id:{}", id);
        return Result.success(userService.queryById(id));
    }

    @ApiOperation("添加用户")
    @PostMapping("/save")
    public Result saveUser(@RequestBody User user) {
        userService.save(user);
        return Result.success("新增成功");
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("/queryById/{id}")
    public Result getById(@PathVariable Long id) {
        User user = userService.queryById(id);
        return Result.success(user);
    }

    @ApiOperation("条件分页查询用户信息")
    @GetMapping("/query")
    public Result query(@RequestParam("pageNum") Integer pageNum,
                        @RequestParam("pageSize") Integer pageSize,
                        String username, String status, Integer roleId, Integer isVip) {
        IPage<UserExtend> p = userService.query(pageNum, pageSize, username, status, roleId, isVip);
        return Result.success(p);
    }
    @ApiOperation(value = "更新用户信息", notes = "id必须存在且有效，如果username存在则必须唯一")
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success("更新成功");
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result deleteByBatch(@PathVariable("ids") List<Long> ids) {
        userService.deleteByBatch(ids);
        return Result.success("删除成功");
    }

}

